<?
class MAIN_Calendar
{
	 public function getCalendar($month,$year,$bEvents=null){
		$nDayToday = date("j");
		if (!is_numeric($month) || !is_numeric($year)) {
			$intThisMonth = date('n');
			$intThisYear = date('Y');
		} else {
			$intThisMonth = $month;
			$intThisYear  = $year;
		}
		$this->ThisMonth = $intThisMonth;
		$this->ThisYear  = $intThisYear;
		$smonth = array ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
		$strMonthName = $smonth[$intThisMonth-1];
		$dt = mktime (0,0,0,$intThisMonth, 1, $intThisYear);
		$intFirstWeekDay = (int) date('w', $dt);// + 1;
		$intFirstWeekDay++;

		$intLastDay = self::GetLastDayofMonth($intThisMonth, $intThisYear);
		$intPrevMonth = $intThisMonth - 1;
		if ($intPrevMonth == 0) {
			$intPrevMonth = 12;
			$intPrevYear = $intThisYear - 1;
		} else {
			$intPrevYear = $intThisYear;
		}
		$intNextMonth = $intThisMonth + 1;
		if ($intNextMonth > 12) {
			$intNextMonth = 1;
			$intNextYear = $intThisYear + 1;
		} else {
			$intNextYear = $intThisYear;
		}
		$LastMonthDate = self::GetLastDayofMonth($intLastMonth, $intPrevYear) - $intFirstWeekDay + 2;
		$NextMonthDate = 1;
		$intPrintDay = 1;
		$dtFirstDay = mktime (0,0,0, $intThisMonth , 1, $intThisYear);
		$dtLastDay 	= mktime (0,0,0, $intThisMonth , $intLastDay, $intThisYear);
		$this->i=0;
		while (!$EndRows) {
			$this->i++;
				$html .="	<tr> ";
				for ($intLoopDay = 1; $intLoopDay <= 7; $intLoopDay++) { //cSUN To cSAT
					if ($intFirstWeekDay > 1) {
						$html .=self::Write_TD ("&nbsp;", "NON", 0);
						$LastMonthDate++;
						$intFirstWeekDay--;
					} else {
						if ($intPrintDay > $intLastDay) {
							$html .=self::Write_TD ("&nbsp;", "NON", 0);
							$NextMonthDate++;
							$EndRows = True;
						} else {
							if ($intPrintDay == $intLastDay) {
								$EndRows = True;
							}
							$dToday = $intPrintDay;
							if (!$bEvents[$intThisYear."-".$intThisMonth."-".sprintf("%02d", $intPrintDay)]) {
								//echo sprintf("%02d", $intPrintDay)."-".$intThisMonth."-".$intThisYear."<br>";
								$html .=self::Write_TD ($intPrintDay , "SOME", $intPrintDay);//<a href=\"index.php?day=$intPrintDay&month=$intThisMonth&year=$intThisYear\">$intPrintDay</a>
							}else
								$html .=self::Write_TD ("<a href=\"#\" onClick=\"app.gotoview('frontend/calendar/display/day/$intPrintDay/month/$intThisMonth/year/$intThisYear');\"><font color='red'>$intPrintDay</font></a>" , "SOME", $intPrintDay);//
						}
						$intPrintDay++;
					}
				}
				$html .="</tr><tr><td colspan=\"13\"><img src=\"".IMG_URL."/calandar/calendar09.gif\" width=\"2\" height=\"2\" /></td></tr>";
			}

		$_html[html] = $html;
		$_html[strMonthName] = $strMonthName;
		$_html[intThisYear] = $intThisYear;
		$_html[intThisMonth] = $intThisMonth;
		$html = self::GetHTML($_html);
		return $html;
	 }
	 public function Write_TD($sValue, $sClass, $nDay) {
		$nDayToday = date("j");
		$intThisMonth = $this->ThisMonth;
		if($this->i>1) $td="<td class=\"style46\"><div align=\"center\"><img src=\"".IMG_URL."/calandar/calendar09.gif\" width=\"2\" height=\"2\" /></div></td>";
	    if(!$sValue) $bg = IMG_URL."/calandar/calendar05.jpg"; else $bg = IMG_URL."/calandar/calendar07.jpg";
	    if (($nDay == $nDayToday) && ($intThisMonth == date("n")))
			return "<td width=\"19\" height=\"19\" background=\"".IMG_URL."/calandar/calendar06.jpg\" class=\"style47\"><div align='center'>$sValue</div></td>".$td;
		else
			return "<td width=\"19\" height=\"19\" class=\"style46\" background='{$bg}'><div align=\"center\"><font color=\"#000000\">$sValue</font></div></td>".$td;
	}
	public function selectmonth($num,$year)
	{
		$st=$year%4;
		$st2=$year%100;
		$st3=$year%400;
		if($num==01)
			 $selectmonth=31;
		elseif($num==02)
			 $selectmonth=28;
		elseif($num==02 and $st==0 and $st2<>0 or $st3==0)
				 $selectmonth=29;
		elseif($num==03)
				 $selectmonth=31;
		elseif($num==04)
				 $selectmonth=30;
		elseif($num==05)
				  $selectmonth=31;
		elseif($num==06)
				 $selectmonth=30;
		elseif($num==07)
			 $selectmonth=31;
		elseif($num==8)
				$selectmonth=31;
		elseif($num==9)
				 $selectmonth=30;
		elseif($num==10)
				$selectmonth=31;
		elseif($num==11)
				 $selectmonth=30;
		elseif($num==12)
				 $selectmonth=31;
		return $selectmonth;
	}

	public function GetLastDayofMonth($intMonthNum, $intYearNum) {
		$lastdate = mktime(0,0,0,$intMonthNum+1,0,$intYearNum);
		return (int) date("j", $lastdate);
	}
	protected function GetHtml($_html){
		$next = self::gerMonth(($_html[intThisMonth]+1),$_html[intThisYear]);
		list($nYear,$nMonth) = explode("-",$next);
		$previos = self::gerMonth(($_html[intThisMonth]-1),$_html[intThisYear]);
		list($pYear,$pMonth) = explode("-",$previos);
		$html = "<table width=\"159\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" bgcolor=\"#FFFFFF\">
				  <tr>
				    <td height=\"10\" valign=\"top\" background=\"".IMG_URL."/calandar/calendar02.jpg\"><img src=\"".IMG_URL."/calandar/calendar02.jpg\" width=\"2\" height=\"10\" /></td>
				    <td height=\"10\" align=\"left\" valign=\"top\" background=\"".IMG_URL."/calandar/calendar01.jpg\"><img src=\"".IMG_URL."/calandar/calendar01.jpg\" width=\"8\" height=\"10\" /></td>
				    <td height=\"10\" valign=\"top\" background=\"".IMG_URL."/calandar/calendar02.jpg\"><img src=\"".IMG_URL."/calandar/calendar02.jpg\" width=\"2\" height=\"10\" /></td>
				  </tr>
				  <tr>
				    <td width=\"2\" rowspan=\"4\" align=\"left\" valign=\"top\" background=\"".IMG_URL."/calandar/calendar02.jpg\"><img src=\"".IMG_URL."/calandar/calendar02.jpg\" width=\"2\" height=\"10\" /></td>
				    <td bordercolor=\"#000000\" bgcolor=\"#FFFFFF\"><img src=\"".IMG_URL."/calandar/space08.gif\" width=\"14\" height=\"7\" /></td>
				    <td width=\"2\" rowspan=\"4\" align=\"right\" valign=\"top\" background=\"".IMG_URL."/calandar/calendar02.jpg\"><img src=\"".IMG_URL."/calandar/calendar02.jpg\" width=\"2\" height=\"10\" /></td>
				  </tr>
				  <tr>
				    <td><table width=\"90%\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">
				      <tr>
				        <td width=\"12\" height=\"22\" align=\"left\" valign=\"top\"><a href='javascript:void(0);' onClick=\"RederCalendar('{$pMonth}','{$pYear}')\"><img src=\"".IMG_URL."/calandar/calendar03.jpg\" width=\"12\" height=\"13\" border=0 /></a></td>
				        <td valign=\"top\"><div align=\"center\" class=\"style40\"><font color=\"#000000\">".$_html[strMonthName].", ".$_html[intThisYear]."</font></div></td>
				        <td width=\"12\" align=\"right\" valign=\"top\"><a href='javascript:void(0);' onClick=\"RederCalendar('{$nMonth}','{$nYear}')\"><img src=\"".IMG_URL."/calandar/calendar04.jpg\" width=\"12\" height=\"13\" border=0 /></a></td>
				      </tr>
				    </table></td>
				  </tr>
				  <tr>
				    <td height=\"141\" valign=\"top\">
					<table width=\"145\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">
				      <tr>
				        <td height=\"17\" valign=\"top\" class=\"style45\"><div align=\"center\">S</div></td>
				        <td rowspan=\"2\" valign=\"top\" class=\"style45\"><div align=\"center\"><img src=\"".IMG_URL."/calandar/calendar09.gif\" width=\"2\" height=\"2\" /></div></td>
				        <td valign=\"top\" class=\"style45\"><div align=\"center\">M</div></td>
				        <td rowspan=\"2\" valign=\"top\" class=\"style45\"><div align=\"center\"><img src=\"".IMG_URL."/calandar/calendar09.gif\" width=\"2\" height=\"2\" /></div></td>
				        <td valign=\"top\" class=\"style45\"><div align=\"center\">T</div></td>
				        <td rowspan=\"2\" valign=\"top\" class=\"style45\"><div align=\"center\"><img src=\"".IMG_URL."/calandar/calendar09.gif\" width=\"2\" height=\"2\" /></div></td>
				        <td valign=\"top\" class=\"style45\"><div align=\"center\">W</div></td>
				        <td rowspan=\"2\" valign=\"top\" class=\"style45\"><div align=\"center\"><img src=\"".IMG_URL."/calandar/calendar09.gif\" width=\"2\" height=\"2\" /></div></td>
				        <td valign=\"top\" class=\"style45\"><div align=\"center\">T</div></td>
				        <td rowspan=\"2\" valign=\"top\" class=\"style45\"><div align=\"center\"><img src=\"".IMG_URL."/calandar/calendar09.gif\" width=\"2\" height=\"2\" /></div></td>
				        <td valign=\"top\" class=\"style45\"><div align=\"center\">F</div></td>
				        <td rowspan=\"2\" valign=\"top\" class=\"style45\"><div align=\"center\"><img src=\"".IMG_URL."/calandar/calendar09.gif\" width=\"2\" height=\"2\" /></div></td>
				        <td valign=\"top\" class=\"style45\"><div align=\"center\">S</div></td>
				      </tr>".$_html[html]."</table></td></tr>" .
				   "<tr>
    					<td><img src=\"".IMG_URL."/calandar/space08.gif\" width=\"14\" height=\"7\" /></td>
				   </tr>
				   <tr>
				    <td valign=\"bottom\" background=\"".IMG_URL."/calandar/calendar08.jpg\"><img src=\"".IMG_URL."/calandar/calendar08.jpg\" width=\"2\" height=\"2\" /></td>
				    <td height=\"2\" align=\"left\" valign=\"bottom\" background=\"".IMG_URL."/calandar/calendar08.jpg\"><img src=\"".IMG_URL."/calandar/calendar08.jpg\" width=\"2\" height=\"2\" /></td>
				    <td valign=\"bottom\" background=\"".IMG_URL."/calandar/calendar08.jpg\"><img src=\"".IMG_URL."/calandar/calendar08.jpg\" width=\"2\" height=\"2\" /></td>
				   </tr></table>";
		return $html;
	}
	protected function gerMonth($month, $year){return date('Y-m', mktime(0, 0, 0, $month, 01, $year));}
}
?>